<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class ZlcrAstrNotAnswerQuestion extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_astr_not_answer_question';

    protected $cacheModel = 'custom_report.astr_not_answer_question';

    /**
     * 获取所以未回答的问题ID
     * @param int $astrId
     * @return array
     * @throws \Exception
     */
    public function getNotAnswerQids(int $astrId): array
    {
        $date = (int)(date('Ymd'));

        $qidsData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $date)
            ->setPage(-1)
            ->setListKeyType($astrId)// 类型 用于分页 例如 0全部 1待支付 已支付 //
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($date, $astrId)
            {
                try
                {
                    $qids = $this->where('astr_id', $astrId)->where('date', $date)->column('qid');
                    $idData = [];

                    foreach ($qids as $qid)
                    {
                        $idData[$qid] = ['id' => $qid];
                    }
                }
                catch (\Exception $e)
                {
                    $idData = [];
                }

                return $idData;
            }, 'id', 0);

        return $qidsData['data'] ? array_keys($qidsData['data']) : [];
    }

    /**
     * 添加拒绝回答问题
     * @param array $map
     * @return bool
     */
    public function notAnswerQuestionAdd(array $map)
    {
        try
        {
            $data = [
                'astr_id' => $map['astr_id'],
                'qid' => $map['qid'],
                'type' => $map['type'] ?? 1,
                'remark' => $map['remark'],
                'date' => (int)(date('Ymd')),
                'create_time' => time(),
            ];

            $this->save($data);

            $this->listDataAdd($data['date'], [$data['qid'] => $data['qid']], $data['astr_id']);
        }
        catch (\Exception $e)
        {
            Log::write('占星师拒绝回答问题失败', 'error', true);
            return false;
        }

        return true;
    }
}
